#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/9/17 14:13
# @Author  : youfeng

import redis

from ext.redis_key_wrap import RedisKeyWrap


class RedisScheduleHandler(object):
    __SCHEDULE_EXPIRE_TIME = 60

    def __init__(self, host, port, password, db, max_connections):
        self.__redis_client = redis.StrictRedis(host=host,
                                                port=port,
                                                password=password,
                                                db=db,
                                                max_connections=max_connections)

    # 判断是否已经在调度
    def is_gsxt_schedule(self, company):
        key = RedisKeyWrap.get_gsxt_schedule_key(company)
        return self.__redis_client.exists(key)

    def is_wenshu_schedule(self, company):
        key = RedisKeyWrap.get_wenshu_schedule_key(company)
        return self.__redis_client.exists(key)

    def is_news_schedule(self, company):
        key = RedisKeyWrap.get_news_schedule_key(company)
        return self.__redis_client.exists(key)

    def is_zhixing_schedule(self, company):
        key = RedisKeyWrap.get_zhixing_schedule_key(company)
        return self.__redis_client.exists(key)

    def is_shixin_schedule(self, company):
        key = RedisKeyWrap.get_shixin_schedule_key(company)
        return self.__redis_client.exists(key)

    def __add_to_redis(self, key):
        self.__redis_client.setex(key, self.__SCHEDULE_EXPIRE_TIME, 1)

    # 添加到调度
    def add_to_gsxt_schedule(self, company):
        self.__add_to_redis(RedisKeyWrap.get_gsxt_schedule_key(company))

    def add_to_news_schedule(self, company):
        self.__add_to_redis(RedisKeyWrap.get_news_schedule_key(company))

    def add_to_zhixing_schedule(self, company):
        self.__add_to_redis(RedisKeyWrap.get_zhixing_schedule_key(company))

    def add_to_shixin_schedule(self, company):
        self.__add_to_redis(RedisKeyWrap.get_shixin_schedule_key(company))

    def add_to_wenshu_schedule(self, company):
        self.__add_to_redis(RedisKeyWrap.get_wenshu_schedule_key(company))

    # 删除调度
    def del_gsxt_schedule(self, company):
        self.__redis_client.delete(RedisKeyWrap.get_gsxt_schedule_key(company))

    def del_news_schedule(self, company):
        self.__redis_client.delete(RedisKeyWrap.get_news_schedule_key(company))

    def del_zhixing_schedule(self, company):
        self.__redis_client.delete(RedisKeyWrap.get_zhixing_schedule_key(company))

    def del_shixin_schedule(self, company):
        self.__redis_client.delete(RedisKeyWrap.get_shixin_schedule_key(company))

    def del_wenshu_schedule(self, company):
        self.__redis_client.delete(RedisKeyWrap.get_wenshu_schedule_key(company))
